package com.chisheng.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

@Aspect
public class LogAspect {

    /* 切面定义
     * 1) @Aspect 注解用于声明切面类
     * 2) @Aspect 注解注解用于声明切点表达式
     * 3) @Before 注解声明前置通知
     * 4) @After 注解声明后置通知
     * 5) @AfterReturning 注解声明返回后置通知
     * 6) @AfterThrowing 注解声明异常后置通知
     * 7) @Around 注解声明环绕通知 */

    @Pointcut("execution(* com.chisheng.service..*.*(..))")
    public void calculateAspect() {
        /* com.chisheng.service 下所有类的日志切点 */
    }

    /* JoinPoint: 连接点，执行的切入点。提供对连接点可用状态和关于连接点的静态信息的反射访问 */

    @Before("calculateAspect()")
    public void beforeInvoke(JoinPoint point) {
        System.out.printf("[%s] - Method name: %s, Method args: %s%n", "beforeInvoke", point.getSignature().getName(),
                Arrays.toString(point.getArgs()));
    }

    @After("calculateAspect()")
    public void afterInvoke(JoinPoint point) {
        System.out.printf("[%s] - Method name: %s%n", "afterInvoke", point.getSignature().getName());
    }

    @AfterReturning(value = "calculateAspect()", returning = "returned")
    public void afterInvokeReturning(/* JoinPoint 参数必须在第一个位置 */ JoinPoint point, Object returned) {
        System.out.printf("[%s] - Method name: %s, Method returned: %s%n", "afterInvokeReturning", point.getSignature()
                .getName(), returned.toString());
    }

    @AfterThrowing(value = "calculateAspect()", throwing = "throwable")
    public void afterInvokeThrowing(/* JoinPoint 参数必须在第一个位置 */ JoinPoint point, Exception throwable) {
        System.out.printf("[%s] - Method name: %s, Method throwing: %s%n", "afterInvokeThrowing", point.getSignature()
                .getName(), throwable.getMessage());
    }

}
